﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rotempco.BaseClass.Objects.EntityObjects;
using Rotempco.Website.WebPages.Admin.Users.Extensions;
using Rotempco.BaseClass;
using Rotempco.Core.Logic;
using Rotempco.BaseClass.Objects.Enums;
using Rotempco.Website.Objects;
using Rotempco.BaseClass.Objects.ReturnObjects;
using Rotempco.Core.Web.CRUD.ACRUD;
using Rotempco.Core.ORM.Objects;
using Rotempco.Core.ORM.Managers;
using Rotempco.BaseClass.Objects.ResultObjects;

namespace Rotempco.Website.WebPages.Admin.Users.Managers
{
    public class LoginUserExtensionManager : ACRUDManager<LoginUserExtension, long>
    {
        #region [ Singleton ]

        static readonly LoginUserExtensionManager _Instance = new LoginUserExtensionManager();

        static LoginUserExtensionManager()
        {
        }

        private LoginUserExtensionManager()
        {
        }

        public static LoginUserExtensionManager Instance
        {
            get
            {
                return _Instance;
            }
        }

        #endregion

        public override LoginUserExtension GetById(long id, bool getDeleted = false)
        {
            LoginUserExtension loginUserExtension = GenericCloner<LoginUser, LoginUserExtension>.Clone(Factory.LoginUser.GetById(id, getDeleted));

            return loginUserExtension;
        }

        public LoginUserExtension GetNewInstance()
        {
            LoginUserExtension loginUserExtension = new LoginUserExtension();
            return loginUserExtension;
        }

        public List<LoginUserExtension> GetAll(PagingContainer pagingContainer)
        {
            List<LoginUser> result = null;

            PagingManager.Instance.Page(pagingContainer, () =>
            {
                result = Factory.LoginUser.GetAll();
            });


            List<LoginUserExtension> loginUserExtensionList = GenericCloner<LoginUser, LoginUserExtension>.Clone(result);

            return loginUserExtensionList;
        }

        public override void WriteToDB(LoginUserExtension loginUserExtension)
        {
            if (loginUserExtension.LoginUserId == 0)
            {
                CreateNewLoginUserResult createNewLoginUserResult = Factory.LoginUser.CreateNewLoginUser(loginUserExtension.Name, loginUserExtension.Email);
                if (createNewLoginUserResult.CreateNewLoginUserResultEnum == CreateNewLoginUserResultEnum.Success)
                    GenericCloner<LoginUser, LoginUserExtension>.Clone(createNewLoginUserResult.LoginUser, loginUserExtension);
                else
                    throw new Exception("User not created: " + createNewLoginUserResult.CreateNewLoginUserResultEnum);
            }
            else
                Factory.LoginUser.WriteToDB(loginUserExtension);

        }

    }
}